package com.zsh.weather.ln.home.ui.home.presenter

import com.amap.api.location.AMapLocationClient
import com.yechaoa.yutilskt.LogUtilKt
import com.zsh.weather.ln.app.App
import com.zsh.weather.ln.base.BasePresenter
import com.zsh.weather.ln.base.OnLoadDataListener
import com.zsh.weather.ln.bean.IndicesBase
import com.zsh.weather.ln.bean.NowBase
import com.zsh.weather.ln.home.ui.home.contract.HomeContract
import com.zsh.weather.ln.home.ui.home.model.HomeModel
import com.zsh.weather.ln.utils.LocationUtils

class HomePresenter : BasePresenter<HomeContract.View>, HomeContract.Presenter {
    private var mLocationClient: AMapLocationClient

    constructor() {
        mLocationClient = AMapLocationClient(App.instance)
    }

     fun getIndices(location: String) {
        HomeModel().getIndices(location, object : OnLoadDataListener {
            override fun <T> success(result: T) {
                getView()!!.indices(result as IndicesBase)
            }
            override fun fail(error: String) {
                LogUtilKt.d("请求失败。。。。。。。" + error)
            }
        })
    }

    override fun getNowWeather() {
        LocationUtils.location(mLocationClient, {
            if (it.errorCode == 0) {
                LogUtilKt.d("定位成功................")
                var longitude = it.longitude
                var latitude = it.latitude
                getNowWeather("${longitude},${latitude}")
                getIndices("${longitude},${latitude}")
                mLocationClient!!.onDestroy()
            } else {
                getNowWeather("116.41,39.92")
                getIndices("116.41,39.92")
                LogUtilKt.d("定位失败................")
                mLocationClient!!.onDestroy()
            }
        })
    }
    private fun getNowWeather(location: String) {
        HomeModel().getNowWeather(location, object : OnLoadDataListener {
            override fun <T> success(result: T) {
                getView()!!.nowWeather(result as NowBase)
            }
            override fun fail(error: String) {
                LogUtilKt.d("请求失败。。。。。。。" + error)
            }
        })
    }

}